package ut.data;
import ut.util.Cache;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
public class Record{
	public static Record getRecord(int id){
		Record record;
		if((record = Cache.getRecord(id)) != null){
			return record;
		}
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try{

			con = Database.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery("select * from record where id=" + id);
			if(rs.next()){
				record = new Record(rs.getInt("free_response"), rs.getInt("free_success"), rs.getInt("response"), rs.getInt("success"), rs.getInt("score_number"), rs.getDouble("score"), rs.getInt("free_task"), rs.getInt("free_task_success"), rs.getInt("task"), rs.getInt("task_success"), rs.getInt("task_score_number"), rs.getDouble("task_score"));
				Cache.cacheRecord(id, record);
				return record;
			}else{
				return null;
			}
		}catch(NamingException e){
			e.printStackTrace();
			return null;
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}finally{
			try{
				if(con != null){
					con.close();
				}
				if(stmt != null){
					stmt.close();
				}
				if(rs != null){
					rs.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
				return null;
			}
		}
	}
	/* Instance Definition */
	public int freeResponse, freeSuccess; // 免費案件的譯者紀錄
	public int response, success, scoreNumber; // 付費案件的譯者紀錄
	public double score; // 付費案件的譯者評價
	public int freeTask, freeTaskSuccess; // 免費案件的案主紀錄
	public int task, taskSuccess, taskScoreNumber; // 付費案件的案主紀錄
	public double taskScore; // 付費案件的案主評價
	public Record(int freeResponse, int freeSuccess, int response, int success, int scoreNumber, double score, int freeTask, int freeTaskSuccess, int task, int taskSuccess, int taskScoreNumber, double taskScore){
		this.freeResponse = freeResponse;
		this.freeSuccess = freeSuccess;
		this.response = response;
		this.success = success;
		this.scoreNumber = scoreNumber;
		this.score = score;
		this.freeTask = freeTask;
		this.freeTaskSuccess = freeTaskSuccess;
		this.task = task;
		this.taskSuccess = taskSuccess;
		this.taskScoreNumber = taskScoreNumber;
		this.taskScore = taskScore;
	}
}